import { defHttp } from '/@/utils/http/axios';
import { getMenuListResultModel, MenuItem } from './model/menuModel';

enum Api {
  GetMenuList = '/api/permission/get_menu_list',
  GetMenuTree = '/api/permission',
  AddMenu = '/api/permission',
  UpdateMenu = '/api/permission/<pk>',
  GetMenuDetail = '/api/permission/<pk>',
}

/**
 * @description: Get user menu based on id
 */

export const getMenuList = () => {
  return defHttp.get<getMenuListResultModel>({ url: Api.GetMenuList });
};

export const getMenuTree = (params: any) => {
  return defHttp.get<MenuItem[]>({ url: Api.GetMenuTree, params }, { withToken: true });
};

export const getMenuDetail = (pk: number) => {
  return defHttp.get<MenuItem>(
    { url: Api.GetMenuDetail.replace('<pk>', String(pk)) },
    { withToken: true },
  );
};

export const addMenu = (data: MenuItem) => {
  return defHttp.post<MenuItem>(
    {
      url: Api.AddMenu,
      data,
    },
    {
      withToken: true,
    },
  );
};

export const updateMenu = (data: MenuItem) => {
  return defHttp.put<MenuItem>(
    {
      url: Api.UpdateMenu.replace('<pk>', String(data.id)),
      data,
    },
    {
      withToken: true,
    },
  );
};
